<?php
/**
 * 网站模块整合生成器
 * 根据用户选择的组件生成完整的网站页面
 */

// 检查是否有POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 获取用户选择的组件
    $selected_components = isset($_POST['components']) ? $_POST['components'] : [];
} else {
    // 默认选择所有组件
    $selected_components = [
        'navbar', 'searchbar', 'hero', 'services', 'cases', 
        'about', 'team', 'stats', 'pricing', 'faq', 
        'contact_cards', 'subscribe', 'contact_form', 'footer'
    ];
}

// 组件标题映射
$component_titles = [
    'navbar' => '导航栏',
    'searchbar' => '头部搜索',
    'hero' => '英雄区域',
    'services' => '服务模块',
    'cases' => '成功案例',
    'about' => '关于我们',
    'team' => '团队介绍',
    'stats' => '数据统计',
    'pricing' => '费用卡片',
    'faq' => '常见问题',
    'contact' => '联系卡片',
    'subscribe' => '订阅模块',
    'contact_form' => '联系我们',
    'footer' => '底部模块',
    'stepper' => '步进器',
    'timeline' => '时间轴',
    'login' => '登录模块',
    'comments' => '评论模块',
    'feedback' => '反馈表单',
    'containers' => '容器模块'
];

// 如果是生成页面请求
if (isset($_POST['generate'])) {
    // 生成网站页面
    generate_website($selected_components);
    exit;
}

/**
 * 生成网站页面
 * @param array $components 选择的组件列表
 */
function generate_website($components) {
    // 设置HTTP头
    header('Content-Type: text/html; charset=utf-8');
    
    // 开始输出HTML
    echo '<!DOCTYPE html>';
    echo '<html lang="zh-CN">';
    echo '<head>';
    echo '<meta charset="UTF-8">';
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
    echo '<title>智联科技 - 企业数字化转型专家</title>';
    
    // 引入外部资源
    echo '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">';
    echo '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">';
    echo '<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>';
    echo '<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>';
    
    // 基础样式
    echo '<style>';
    echo 'body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }';
    echo '.bg-primary { background-color: #0066cc !important; }';
    echo '.text-primary { color: #0066cc !important; }';
    echo '.bg-dark { background-color: #1a202c !important; }';
    echo '.text-dark { color: #1a202c !important; }';
    echo '.bg-light { background-color: #f8f9fa !important; }';
    echo '.rounded-2xl { border-radius: 1rem; }';
    echo '</style>';
    
    echo '</head>';
    echo '<body>';
    
    // 循环加载选中的组件
    foreach ($components as $component) {
        $component_path = "../components/{$component}.html";
        if (file_exists($component_path)) {
            include $component_path;
        }
    }
    
    // JavaScript代码
    echo '<script>';
    echo '// 移动端菜单切换';
    echo 'document.addEventListener("DOMContentLoaded", function() {';
    echo '  const menuToggle = document.querySelector(".menu-toggle");';
    echo '  const mobileMenu = document.querySelector(".mobile-menu");';
    echo '  ';
    echo '  if (menuToggle && mobileMenu) {';
    echo '    menuToggle.addEventListener("click", function() {';
    echo '      mobileMenu.classList.toggle("hidden");';
    echo '      const icon = menuToggle.querySelector("i");';
    echo '      if (icon.classList.contains("fa-bars")) {';
    echo '        icon.classList.remove("fa-bars");';
    echo '        icon.classList.add("fa-times");';
    echo '      } else {';
    echo '        icon.classList.remove("fa-times");';
    echo '        icon.classList.add("fa-bars");';
    echo '      }';
    echo '    });';
    echo '  }';
    echo '  ';
    echo '  // 导航栏滚动效果';
    echo '  const navbar = document.querySelector(".navbar");';
    echo '  if (navbar) {';
    echo '    window.addEventListener("scroll", function() {';
    echo '      if (window.scrollY > 50) {';
    echo '        navbar.classList.add("bg-white", "shadow-md");';
    echo '        navbar.classList.remove("bg-transparent");';
    echo '      } else {';
    echo '        navbar.classList.remove("bg-white", "shadow-md");';
    echo '        navbar.classList.add("bg-transparent");';
    echo '      }';
    echo '    });';
    echo '  }';
    echo '  ';
    echo '  // 平滑滚动';
    echo '  document.querySelectorAll(\'a[href^="#"]\').forEach(anchor => {';
    echo '    anchor.addEventListener(\'click\', function(e) {';
    echo '      e.preventDefault();';
    echo '      const targetId = this.getAttribute(\'href\');';
    echo '      if (targetId === \'#\') return;';
    echo '      const targetElement = document.querySelector(targetId);';
    echo '      if (targetElement) {';
    echo '        window.scrollTo({';
    echo '          top: targetElement.offsetTop - 80, // 考虑导航栏高度';
    echo '          behavior: \'smooth\''; 
    echo '        });';
    echo '        // 关闭移动菜单（如果打开）';
    echo '        if (mobileMenu && !mobileMenu.classList.contains(\'hidden\')) {';
    echo '          mobileMenu.classList.add(\'hidden\');';
    echo '          if (menuToggle) {';
    echo '            const icon = menuToggle.querySelector(\'i\');';
    echo '            icon.classList.remove(\'fa-times\');';
    echo '            icon.classList.add(\'fa-bars\');';
    echo '          }';
    echo '        }';
    echo '      }';
    echo '    });';
    echo '  });';
    echo '  ';
    echo '  // 返回顶部按钮';
    echo '  const backToTopButton = document.getElementById(\'back-to-top\');';
    echo '  if (backToTopButton) {';
    echo '    window.addEventListener(\'scroll\', function() {';
    echo '      if (window.scrollY > 300) {';
    echo '        backToTopButton.classList.remove(\'opacity-0\', \'invisible\');';
    echo '        backToTopButton.classList.add(\'opacity-100\', \'visible\');';
    echo '      } else {';
    echo '        backToTopButton.classList.add(\'opacity-0\', \'invisible\');';
    echo '        backToTopButton.classList.remove(\'opacity-100\', \'visible\');';
    echo '      }';
    echo '    });';
    echo '    ';
    echo '    backToTopButton.addEventListener(\'click\', function() {';
    echo '      window.scrollTo({';
    echo '        top: 0,'; 
    echo '        behavior: \'smooth\''; 
    echo '      });';
    echo '    });';
    echo '  }';
    echo '  ';
    echo '  // 案例筛选';
    echo '  const filterButtons = document.querySelectorAll(\'#cases .flex-wrap button\');';
    echo '  if (filterButtons.length > 0) {';
    echo '    filterButtons.forEach(button => {';
    echo '      button.addEventListener(\'click\', function() {';
    echo '        // 移除所有按钮的活跃状态';
    echo '        filterButtons.forEach(btn => {';
    echo '          btn.classList.remove(\'bg-primary\', \'text-white\');';
    echo '          btn.classList.add(\'bg-white\', \'text-dark\');';
    echo '        });';
    echo '        ';
    echo '        // 添加当前按钮的活跃状态';
    echo '        this.classList.remove(\'bg-white\', \'text-dark\');';
    echo '        this.classList.add(\'bg-primary\', \'text-white\');';
    echo '        ';
    echo '        // 这里可以添加实际的筛选逻辑';
    echo '        const category = this.textContent.trim();';
    echo '        console.log(`筛选类别: ${category}`);';
    echo '        // 实际项目中，这里应该根据category筛选案例';
    echo '      });';
    echo '    });';
    echo '  }';
    echo '  ';
    echo '  // 数据图表';
    echo '  const statsChart = document.getElementById(\'stats-chart\');';
    echo '  if (statsChart) {';
    echo '    new Chart(statsChart, {';
    echo '      type: \'bar\',';
    echo '      data: {';
    echo '        labels: [\'2019\', \'2020\', \'2021\', \'2022\', \'2023\'],';
    echo '        datasets: [{';
    echo '          label: \'项目数量\',';
    echo '          data: [120, 190, 250, 320, 410],';
    echo '          backgroundColor: \'#0066cc\',';
    echo '          borderWidth: 0';
    echo '        }]';
    echo '      },';
    echo '      options: {';
    echo '        responsive: true,';
    echo '        plugins: {';
    echo '          legend: {';
    echo '            display: false';
    echo '          }';
    echo '        }';
    echo '      }';
    echo '    });';
    echo '  }';
    echo '  ';
    echo '  // FAQ折叠功能';
    echo '  const faqItems = document.querySelectorAll(\'.faq-item\');';
    echo '  if (faqItems.length > 0) {';
    echo '    faqItems.forEach(item => {';
    echo '      const question = item.querySelector(\'.faq-question\');';
    echo '      const answer = item.querySelector(\'.faq-answer\');';
    echo '      const icon = question.querySelector(\'i\');';
    echo '      ';
    echo '      question.addEventListener(\'click\', function() {';
    echo '        // 切换答案显示状态';
    echo '        answer.classList.toggle(\'hidden\');';
    echo '        ';
    echo '        // 切换图标';
    echo '        if (icon.classList.contains(\'fa-chevron-down\')) {';
    echo '          icon.classList.remove(\'fa-chevron-down\');';
    echo '          icon.classList.add(\'fa-chevron-up\');';
    echo '        } else {';
    echo '          icon.classList.remove(\'fa-chevron-up\');';
    echo '          icon.classList.add(\'fa-chevron-down\');';
    echo '        }';
    echo '      });';
    echo '    });';
    echo '  }';
    echo '  ';
    echo '  // 评分星星功能';
    echo '  const starRatings = document.querySelectorAll(\'.star-rating\');';
    echo '  if (starRatings.length > 0) {';
    echo '    starRatings.forEach(rating => {';
    echo '      const stars = rating.querySelectorAll(\'i\');';
    echo '      let currentRating = 0;';
    echo '      ';
    echo '      // 获取初始评分';
    echo '      stars.forEach((star, index) => {';
    echo '        if (star.classList.contains(\'text-yellow-400\')) {';
    echo '          currentRating = index + 1;';
    echo '        }';
    echo '      });';
    echo '      ';
    echo '      // 添加点击事件';
    echo '      stars.forEach((star, index) => {';
    echo '        star.addEventListener(\'click\', function() {';
    echo '          const ratingValue = index + 1;';
    echo '          updateStars(ratingValue);';
    echo '        });';
    echo '        ';
    echo '        // 添加悬停效果';
    echo '        star.addEventListener(\'mouseover\', function() {';
    echo '          const hoverValue = index + 1;';
    echo '          highlightStars(hoverValue);';
    echo '        });';
    echo '        ';
    echo '        star.addEventListener(\'mouseout\', function() {';
    echo '          highlightStars(currentRating);';
    echo '        });';
    echo '      });';
    echo '      ';
    echo '      function updateStars(value) {';
    echo '        currentRating = value;';
    echo '        highlightStars(value);';
    echo '      }';
    echo '      ';
    echo '      function highlightStars(value) {';
    echo '        stars.forEach((star, index) => {';
    echo '          if (index < value) {';
    echo '            star.classList.add(\'text-yellow-400\');';
    echo '            star.classList.remove(\'text-gray-300\');';
    echo '          } else {';
    echo '            star.classList.remove(\'text-yellow-400\');';
    echo '            star.classList.add(\'text-gray-300\');';
    echo '          }';
    echo '        });';
    echo '      }';
    echo '    });';
    echo '  }';
    echo '});';
    echo '</script>';
    
    echo '</body>';
    echo '</html>';
}
?>